package tech.aistar.controller.noteshow;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import tech.aistar.config.GuidelInterceptor;
import tech.aistar.entity.authordis.NoteAuthor;
import tech.aistar.entity.authordis.UserTable;
import tech.aistar.entity.noteshow.ApplyTraveler;
import tech.aistar.service.applytraveler.IApplyTravelerService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by Dell on 2019/10/2.
 */
@Controller
@Transactional
public class ApplyTravelerController {

    @Autowired
    private IApplyTravelerService applyTravelerService;

    @GuidelInterceptor
    @RequestMapping("/apply")
    public String applyTraveler(HttpServletRequest request, Model model){

        //登录之后,拿到userTable
        HttpSession session=request.getSession();
        UserTable userTable= (UserTable) session.getAttribute("userId");
        model.addAttribute("userTable",userTable);

        NoteAuthor noteAuthor = applyTravelerService.findTraveler(userTable.getId());

        model.addAttribute("noteAuthor",noteAuthor);

        return "traveler_show/apply_traveler";
    }
    /**
     * @Description:验证通过不是达人,填写表单
     * @param:
     * @author XiaoDi
     * @date 2019/10/7 20:50
    */
    @PostMapping("/applyTraveler")
    public String applySuccess( ApplyTraveler applyTraveler){
        applyTravelerService.applyTravler(applyTraveler);

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //更新达人表的信息
        applyTravelerService.addTravler(applyTraveler);

//        更新游记文章表flag的信息
        applyTravelerService.updateTraveler(applyTraveler.getUserId());
        return "traveler_show/apply_traveler_verity";
    }
}
